package com.nycreativity.elecreativity.controller.ijpay.paypal;

import cn.hutool.json.JSONUtil;
import com.ijpay.core.kit.PayKit;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sxt
 * @version 1.0
 * @description PayPal请求参数构造器
 * @date 2021/6/1 4:25 下午
 */
@SuppressWarnings("unused")
public class PayPalRequestData {

    public static final String DEFAULT_CURRENCY_CODE = "USD";
    public static final String CURRENCY_CODE = "currency_code";
    public static final String AMOUNT_VALUE = "value";
    public static final String AMOUNT = "amount";
    public static final String PURCHASE_UNITS = "purchase_units";
    public static final String CANCEL_URL = "cancel_url";
    public static final String RETURN_URL = "return_url";
    public static final String APPLICATION_CONTEXT = "application_context";
    public static final String DEFAULT_INTENT = "CAPTURE";
    public static final String INVOICE_ID = "invoice_id";
    public static final String INTENT = "intent";
    public static final String NOTE_TO_PAYER = "note_to_payer";

    public static class CreateDataBuilder {

        private final Map<String, Object> dataMap = new HashMap<>();

        private final ArrayList<Map<String, Object>> list = new ArrayList<>();

        private final Map<String, Object> amount = new HashMap<>();

        private final Map<String, String> applicationContext = new HashMap<>();

        public CreateDataBuilder() {
            dataMap.put(INTENT, DEFAULT_INTENT);
            amount.put(CURRENCY_CODE, DEFAULT_CURRENCY_CODE);
        }

        public CreateDataBuilder currency(String code) {
            amount.put(CURRENCY_CODE, code);
            return this;
        }

        public CreateDataBuilder amount(Double value) {
            BigDecimal bg = new BigDecimal(value);
            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            amount.put(AMOUNT_VALUE, f1);
            return this;
        }

        public CreateDataBuilder cancelUrl(String url) {
            applicationContext.put(CANCEL_URL, url);
            return this;
        }

        public CreateDataBuilder returnUrl(String url) {
            applicationContext.put(RETURN_URL, url);
            return this;
        }

        public String build() {
            Map<String, Object> itemMap = new HashMap<>();
            itemMap.put(AMOUNT, amount);
            list.add(itemMap);
            dataMap.put(PURCHASE_UNITS, list);
            dataMap.put(APPLICATION_CONTEXT, applicationContext);
            return JSONUtil.toJsonStr(dataMap);
        }
    }

    public static class RefundDataBuilder {

        private final Map<String, Object> map = new HashMap<>();

        private final Map<String, Object> amount = new HashMap<>();

        public RefundDataBuilder() {
            map.put(INVOICE_ID, PayKit.generateStr());
            map.put(NOTE_TO_PAYER, "退款操作");
            amount.put(CURRENCY_CODE, DEFAULT_CURRENCY_CODE);
        }

        public RefundDataBuilder invoiceId(String id) {
            map.put(INVOICE_ID, id);
            return this;
        }

        public RefundDataBuilder noteToPayer(String note) {
            map.put(NOTE_TO_PAYER, note);
            return this;
        }

        public RefundDataBuilder currency(String code) {
            amount.put(CURRENCY_CODE, code);
            return this;
        }

        public RefundDataBuilder amount(Double value) {
            BigDecimal bg = new BigDecimal(value);
            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            amount.put(AMOUNT_VALUE, f1);
            return this;
        }

        public String build() {
            map.put(AMOUNT, amount);
            return JSONUtil.toJsonStr(map);
        }

    }
}
